package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper mapper;




    /**
     * 分页查询
     * @param pageBean
     * @return
     */
    public PageResult findByPage(QueryPageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        //将查询的结果封装为集合对象
        List<Permission> list = mapper.selectByCondition(pageBean.getQueryString());
        //将集合载入到pageInfo中，反推出总页数
        PageInfo pi = new PageInfo(list);
        //将总页数与当前页集合载入到PageResult进行返回
        PageResult result = new PageResult(pi.getTotal(), list);
        return result;
    }

    /**
     * 添加权限
     * @param permission
     */
    public void add(Permission permission) {
        mapper.add(permission);
    }

    /**
     * 编辑
     * @param permission
     */
    public void edit(Permission permission) {
        mapper.edit(permission);
    }

    /**
     * 根据id查询权限信息
     * @param id
     * @return
     */
    public Permission findById(String id) {
        Permission permission = mapper.findById(id);
        return permission ;


    }

    public void del(String id) throws RuntimeException{
        Long permissionCountById = mapper.findPermissionCountById(id);
        if (permissionCountById>0){
            //当前检查项被引用，不能删除
            throw  new RuntimeException("当前检查项被引用，不能删除");
        }
        mapper.del(id);
    }


}
